/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.controller.evaluation.document;

import cn.ac.nercis.pes.common.constant.common.BaseConstants;
import cn.ac.nercis.pes.common.constant.evaluation.InfoLevel;
import cn.ac.nercis.pes.community.utils.IconUtils;
import cn.ac.nercis.pes.community.utils.LogUtils;
import cn.ac.nercis.pes.model.dto.evaluation.analyze.IntegrityVerificationDTO;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import java.util.stream.Stream;

/**
 * 完整性验证控制器
 *
 * @author zpy
 * @version 1.0.0
 */
@Slf4j
public class IntegrityVerificationController implements Initializable {

    @FXML
    private VBox outputBox;

    private List<IntegrityVerificationDTO> integrityVerifications;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
    }


    public void initializeData(List<IntegrityVerificationDTO> integrityVerifications){
        LogUtils.info(log, "初始化完整性验证...");
        this.integrityVerifications = integrityVerifications;
        this.loadMenuData();
    }

    private void loadMenuData() {
        outputBox.getChildren().clear();
        this.integrityVerifications.stream()
                .filter(iv->iv.getLevel()== InfoLevel.ERROR)
                .forEach(this::renderInfo);
        this.integrityVerifications.stream()
                .filter(iv->iv.getLevel()==InfoLevel.WARN)
                .forEach(this::renderInfo);
        this.integrityVerifications.stream()
                .filter(iv->iv.getLevel()==InfoLevel.INFO)
                .forEach(this::renderInfo);
    }

    private void renderInfo(IntegrityVerificationDTO item){
        var itemBox = new VBox();
        itemBox.getChildren().add(generateItem(item));
        if(StringUtils.isNotBlank(item.getDetail())){
            var detailBox = new VBox();
            detailBox.getStyleClass().add("verification-error-detail");
            detailBox.setPadding(new Insets(5, 10, 5, 10));
            Stream.of(item.getDetail().split(BaseConstants.REGEX_WRAP_TEXT_SPLIT))
                    .filter(StringUtils::isNotBlank)
                    .forEach(text->{
                        Label label = new Label(text);
                        label.setWrapText(true);
                        label.setTooltip(new Tooltip(text));
                        detailBox.getChildren().add(label);
                    });
            itemBox.getChildren().add(detailBox);
        }
        outputBox.getChildren().add(itemBox);
    }

    private HBox generateItem(IntegrityVerificationDTO item) {
        HBox hBox = new HBox();
        hBox.setAlignment(Pos.CENTER_LEFT);
        hBox.setPadding(new Insets(10, 10, 10, 10));
        Label nameLabel = new Label(item.getName());
        applyItemStyle(nameLabel, item.getLevel(), true);
        hBox.getChildren().add(nameLabel);
        HBox stateBox = new HBox();
        stateBox.setAlignment(Pos.BASELINE_RIGHT);
        HBox.setHgrow(stateBox, Priority.SOMETIMES);
        Label stateLabel = new Label();
        switch (item.getLevel()) {
            case WARN:
                stateLabel.setText("警告");
                hBox.getStyleClass().add("verification-incomplete-cell");
                break;
            case INFO:
                stateLabel.setText("通过");
                hBox.getStyleClass().add("verification-finish-cell");
                break;
            default:
                stateLabel.setText("错误");
                hBox.getStyleClass().add("verification-error-cell");
                break;
        }
        applyItemStyle(stateLabel, item.getLevel(), false);
        stateBox.getChildren().add(stateLabel);
        hBox.getChildren().add(stateBox);
        return hBox;
    }

    private void applyItemStyle(Label menuLabel, InfoLevel level, boolean icon) {
        switch (level) {
            case WARN:
                if (icon) {
                    menuLabel.setGraphic(IconUtils.getGlyph("incomplete-process-icon","verification-icon"));
                }
                break;
            case INFO:
                if (icon) {
                    menuLabel.setGraphic(IconUtils.getGlyph("finish-process-icon","verification-icon"));
                }
                break;
            default:
                if (icon) {
                    menuLabel.setGraphic(IconUtils.getGlyph("none-process-icon","verification-icon"));
                }
                break;
        }
    }
}